package com.google.gwt.homepage.smile.client.presenter;

import com.google.gwt.homepage.smile.client.SmileServiceAsync;
import com.google.gwt.homepage.smile.client.event.AddContactEvent;
import com.google.gwt.homepage.smile.client.event.EditContactEvent;
import com.google.gwt.homepage.smile.client.event.EnneagramGoResultEvent;
import com.google.gwt.homepage.smile.client.event.EnneagramGoResultEventHandler;
import com.google.gwt.homepage.smile.client.view.EnneagramTestDoView;

import com.google.gwt.homepage.smile.shared.FriendTable;
import com.google.gwt.homepage.smile.shared.ContactDetails;
import com.google.gwt.homepage.smile.shared.EnneagramTestResultBean;
import com.google.gwt.homepage.smile.shared.MemberBean;
import com.google.gwt.homepage.smile.shared.SajuData;
import com.google.gwt.homepage.smile.shared.SajuDataTable;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;


import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

import java.util.ArrayList;
import java.util.List;

public class SajuTestPresenter implements Presenter, ValueChangeHandler<String>{  



  public interface Display {
    HasClickHandlers getSajuResultButton();
    String getInputedYear();
    String getInputedMonth();
    String getInputedDate();
    String getInputedTiem();
    void setSajuResult(SajuDataTable result);
    void invisibleSajuResult();
    
    void setLoadImg();//로드 이미지 설정
    
    HasClickHandlers getSearchFriendButton();

    void showSearchFriendResult(List<FriendTable> result);
    
    //HasClickHandlers getDeleteButton();
    //HasClickHandlers getList();
    //void setData(List<String> data);
    //int getClickedRow(ClickEvent event);
    //List<Integer> getSelectedRows();
	//HasWidgets getContentWidget();//컨텐츠가 들어갈 부분을 가져옴( 이 영역에 EnneagramTestWidget, EnneagramResultWidget가 들어감
	//void showTestResult(EnneagramTestResultBean result);
    Widget asWidget();
  }
  
  private final SmileServiceAsync rpcService;
  private final HandlerManager eventBus;
  private final Display display;
  
  //private static String loadingImage;
  
  public SajuTestPresenter(SmileServiceAsync rpcService, HandlerManager eventBus, Display view) {
    this.rpcService = rpcService;
    this.eventBus = eventBus;
    this.display = view;
  }
  
  public void bind() {
	  History.addValueChangeHandler(this);
	  
	  
	  display.getSajuResultButton().addClickHandler(new ClickHandler() {   
	      public void onClick(ClickEvent event) {
	    	  
	    	    	  
	    	  getSajuResult();
	      }
	    });
	  
	  display.getSearchFriendButton().addClickHandler(new ClickHandler() {   
	      public void onClick(ClickEvent event) {
	    	  
	    	  getSearchFriend();  	  
	    	  
	      }
	    });
	  
	  /*
	    eventBus.addHandler(EnneagramGoResultEvent.TYPE,
	        new EnneagramGoResultEventHandler() {
	          public void onEnneagramGoResult(EnneagramGoResultEvent event) {     	 
	        	  display.showTestResult(event.getResult());
	          }
	        });  
     */  
  }
  
  
  
  /*친구찾기 결과 가져오기*/
  private void getSearchFriend() { 
	  
	  //로그인이 되어있지 않으면 로그인을 하도록함
	  
	  //로그인 되어있으면 이메일을 가져와서 서버로 친구찾기 요청
     
     
      rpcService.getLoginInfo(Window.Location.getPath(), new AsyncCallback<MemberBean>() {
	      public void onSuccess(MemberBean result) {
	    	if(result.getEmail()!=null && result.getEmail().length()>0){ 
	    		requestSearchFriend(result.getEmail());
	    	}else{
	    		Window.alert("로그인하셔야 사용가능합니다.");
	    	}

	      }
	      public void onFailure(Throwable caught) {
	        Window.alert("Error getLoginInfo");
	      }
	    });
  }
  
  
  /*친구찾기 서버로 요청*/
  private void requestSearchFriend(String i_email){
	  rpcService.searchFriend(i_email, new AsyncCallback<List<FriendTable>>() {
	      public void onSuccess(List<FriendTable> result) {
	    	  display.showSearchFriendResult(result);
	      }
	      
	      public void onFailure(Throwable caught) {
	        Window.alert("Error getSearchFriend");
	      }
	    });
  }
  
  
  
  
/*
  public void go(final HasWidgets container) {
    bind();
    container.clear();
    container.add(display.asWidget());
    
    if ("enneagram_test".equals(History.getToken())) {
        History.newItem("testDo");
      }
      else {
        History.fireCurrentHistoryState();
      }
  }
*/
  public void go(final HasWidgets container) {
	  bind();
	    container.clear();
	    container.add(display.asWidget());
	}
  
  
  
  
  
  private void getSajuResult() {
	    String year=display.getInputedYear();
	    String month=display.getInputedMonth();
	    String date = display.getInputedDate();
	    String time = display.getInputedTiem();
	    
	    
	    //기존 사주결과를 않보이게
	    display.invisibleSajuResult();
	    
	    
	    
	    // Show the loading image
	    
	    display.setLoadImg();
	    
	    
	    
	    rpcService.getSajuResult(year, month, date, time, new AsyncCallback<SajuDataTable>() {
	      public void onSuccess(SajuDataTable result) {
	        display.setSajuResult(result);
	      }
	      
	      public void onFailure(Throwable caught) {
	        Window.alert("Error getSajuResult");
	      }
	    });
	  }
  
  

  public void onValueChange(ValueChangeEvent<String> event) {
	    String token = event.getValue();
	  }
  
}
